/*
 * @Author: your name
 * @Date: 2021-08-25 17:31:09
 * @LastEditTime: 2021-09-08 14:01:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bigdate\src\setting\setRouter.ts
 */
import { store } from '../store/index'
import { routeFilter, router } from '../router/index'
import { ROUTER_ARR, ARR_ROUTER } from '../mock/mock'
import { ABOUT_PATH, LOGIN_PAGE } from '../router/moudule/baseic'
import { updateTheme,onChangeMode } from './theme'
import { updateLang } from '../setting/locales/useI18n'
import { GetLocalStrong, SetLocalStrong } from '../ntils/hooks'
import { autoWindow } from './menuTrigger';


  

// 通过在store下获取到路由之后 通过此函数过滤并过滤路由

// 添加到侧边 与 添加路由分开 因为有其他页面不能添加到侧边栏中去比如：login
const APP_THEME_LOCAL = GetLocalStrong('APP_THEME_LOCAL')
const APP_LANG_LOCAL = GetLocalStrong('APP_LANG_LOCAL')
const roles = GetLocalStrong('roles')
// 初始化路由
export function changePermissionCode() {
    //   接直接写在此处  此函数只在 page/index下初始化调用  和登录之后调用 
    let arrs = routeFilter(ARR_ROUTER)
    arrs.forEach(item => { router.addRoute(item) })
    store.state.routerArr = [...arrs, ...ABOUT_PATH]
    let APP_ROUTER = GetLocalStrong('APP_ROUTER') 
    // 判断历史路由是否存在 如果存在直接跳转
    if (!APP_ROUTER) {
        SetLocalStrong({ key: 'APP_ROUTER', value: '/' })
        router.push('/')
    } else {
        router.push(APP_ROUTER)        
    }
      
    // 每次重载路由 需要跳转到首页并重定向到 /index 页面
}

/**
 * //持续集成
 * 初始化阶段执行
 * 此函数只会执行一次
 */
export function getOpenKeepAlive() {
    if (!roles) return
    // 模式
    let APP_MODE = GetLocalStrong('APP_MODE');
    onChangeMode(APP_MODE==='dark');
    // 视口宽度监听
    autoWindow()
    // 集成路由
    changePermissionCode()
    // 集成主题
    if (!APP_THEME_LOCAL) {
        SetLocalStrong({ key: 'APP_THEME_LOCAL', value: 'jinghong' })
        updateTheme('jinghong')
    } else {
        updateTheme(APP_THEME_LOCAL)
    }
    // 集成国际化
    if (!APP_LANG_LOCAL) {
        SetLocalStrong({ key: 'APP_LANG_LOCAL', value: 'ch' })
        updateLang('ch') 
    } else {
        updateLang(APP_LANG_LOCAL)  
    }
}
